{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a02bb216",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = ''\n",
    "os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2508433b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/.local/lib/python3.10/site-packages/bitsandbytes/cextension.py:34: UserWarning: The installed version of bitsandbytes was compiled without GPU support. 8-bit optimizers, 8-bit multiplication, and GPU quantization are unavailable.\n",
      "  warn(\"The installed version of bitsandbytes was compiled without GPU support. \"\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/.local/lib/python3.10/site-packages/bitsandbytes/libbitsandbytes_cpu.so: undefined symbol: cadam32bit_grad_fp32\n",
      "[2023-09-18 08:09:23,077] [INFO] [real_accelerator.py:133:get_accelerator] Setting ds_accelerator to cuda (auto detect)\n"
     ]
    }
   ],
   "source": [
    "from transformers import LlamaConfig, LlamaForCausalLM, AutoTokenizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5098661b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total 8.0K\r\n",
      "drwxr-xr-x 3 ubuntu ubuntu 4.0K Sep 18 06:45 checkpoint-6900\r\n",
      "drwxr-xr-x 3 ubuntu ubuntu 4.0K Sep 18 07:55 checkpoint-6950\r\n"
     ]
    }
   ],
   "source": [
    "!ls -lh fpf-13b-32k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "59dfd6e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LlamaConfig {\n",
       "  \"_name_or_path\": \"mesolitica/llama-13b-hf-16384-fpf\",\n",
       "  \"architectures\": [\n",
       "    \"LlamaForCausalLM\"\n",
       "  ],\n",
       "  \"bos_token_id\": 1,\n",
       "  \"eos_token_id\": 2,\n",
       "  \"hidden_act\": \"silu\",\n",
       "  \"hidden_size\": 5120,\n",
       "  \"initializer_range\": 0.02,\n",
       "  \"intermediate_size\": 13824,\n",
       "  \"max_position_embeddings\": 32768,\n",
       "  \"model_type\": \"llama\",\n",
       "  \"num_attention_heads\": 40,\n",
       "  \"num_hidden_layers\": 8,\n",
       "  \"num_key_value_heads\": 40,\n",
       "  \"pad_token_id\": 0,\n",
       "  \"pretraining_tp\": 1,\n",
       "  \"rms_norm_eps\": 1e-05,\n",
       "  \"rope_scaling\": null,\n",
       "  \"tie_word_embeddings\": false,\n",
       "  \"torch_dtype\": \"bfloat16\",\n",
       "  \"transformers_version\": \"4.31.0\",\n",
       "  \"use_cache\": true,\n",
       "  \"vocab_size\": 32000\n",
       "}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "half_config = LlamaConfig.from_pretrained('fpf-13b-32k/checkpoint-6950', \n",
    "                                             num_hidden_layers = 8)\n",
    "half_config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dde4878f",
   "metadata": {},
   "outputs": [],
   "source": [
    "small = LlamaForCausalLM(half_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05d26450",
   "metadata": {},
   "outputs": [],
   "source": [
    "sum(p.numel() for p in small.parameters())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c89db2de",
   "metadata": {},
   "outputs": [],
   "source": [
    "small"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f102168",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = LlamaForCausalLM.from_pretrained('fpf-13b-32k/checkpoint-6950')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5a3b94f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch.nn as nn\n",
    "\n",
    "def copy_layers(src_layers, dest_layers, layers_to_copy):\n",
    "    layers_to_copy = nn.ModuleList([src_layers[i] for i in layers_to_copy])\n",
    "    assert len(dest_layers) == len(layers_to_copy), f\"{len(dest_layers)} != {len(layers_to_copy)}\"\n",
    "    dest_layers.load_state_dict(layers_to_copy.state_dict())\n",
    "\n",
    "layers_to_copy = [0,1,2,3,4,5,6,7]\n",
    "\n",
    "copy_layers(model.model.layers, small.model.layers, layers_to_copy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc1e0a16",
   "metadata": {},
   "outputs": [],
   "source": [
    "small.model.embed_tokens.load_state_dict(model.model.embed_tokens.state_dict())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "618e59ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "small.model.norm.load_state_dict(model.model.norm.state_dict())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dcc8d85b",
   "metadata": {},
   "outputs": [],
   "source": [
    "small.lm_head.load_state_dict(model.lm_head.state_dict())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5bfa404",
   "metadata": {},
   "outputs": [],
   "source": [
    "small.save_pretrained(\"3b\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c707c7b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained('fpf-13b-32k/checkpoint-6950')\n",
    "tokenizer.save_pretrained(\"3b\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28825e17",
   "metadata": {},
   "outputs": [],
   "source": [
    "!ls -lh 3b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48ee5d51",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
